
// my custom defines
#include "main.h"
 

// packet buffer, incoming and outgoing
u8 g_PktRXBuff[pktBuffSize];
u8 g_PktTXBuff[pktBuffSize];
extern u8 RxCounter;
u8 g_Vars[g_VarsSize];

u8 dataState = 0x01;

int main(void)
{
	int i,j =0;
	u8 packetErr = 0;
	u16 respSize = 0;
	 

	/* basic init stuff */
	// init system stuff in system_stm32f10x.c
	SystemInit();
	// init all our stuff
	initSystems();

	printf(" Starting");
	for(i=0; i<pktBuffSize; i++)
	{
		g_PktTXBuff[i] = 0;
		g_PktRXBuff[i] = 0;
		printf("%d",i);
	}

	// test leds
	for(i=0; i<=0x0F; i++)
	{
		setLeds(i);
		Delay(5);
	}


	while(1)
	{
		setLeds(dataState);
		// Switch on the state of the robot
		switch(dataState)
		{
			case idle: // 
				// set rx index back to 0
				RxCounter = 0;
				// sleep and wake on RX of uart ? 
				dataState = waitingfordata;
				break;

			case waitingfordata: 
				// if we have enough bytes in the incoming buffer to make 
				// a packet than test to see if there is a valid packet in 
				// the buffer
				if(RxCounter >= (hdrSize + crcSize))
				{
					printf("got packet, checking");
					packetErr = checkPacket(g_PktRXBuff, RxCounter);

					//printf("Packet Processing: 0x%X", packetErr);					
					printf("Packet err %d", packetErr);
					switch(packetErr)
					{
						case 0: // no error, valid packet
							dataState = gotnewdata;
							break;
						case 1: // we need more data 
							break;
						case 2: // crc error or invalid packet
							RxCounter = 0;
							break;
					}
					//RxCounter = 0;
					//dataState = gotnewdata;
				}

				// otherwise do something else here
				break;
			
			case gotnewdata:
				// Process the packet, all of the needed vars
				// were saved when we checked the packet
				respSize = processPacket(g_PktRXBuff, RxCounter);
				// if we get a size back there is data ready to be
				// sent out, else there is nothing to respond 
				if(respSize)
					dataState = readytosenddata;
				else 
					dataState = idle;	

				break;

			case readytosenddata:
				// send out the data, todo get right size!
				DMA_TXPacket(g_PktTXBuff, respSize);
				break;

			case sendingdata:
				// more free time here
				break;

			case dataerror:
				// there is an error, just start over
				printf("Error Uart Flags: 0x%X", USART2->SR);
				dataState = idle;
				break;

			default:
				//printf("PacketState: %d\n\r", dataState);
				Delay(100);
		}
		//printf("PacketState: %d\n\r", dataState);
		Delay(100);
		//printf("working");
	}
}

/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(USART3, (uint8_t) ch);

  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET)
  {}

  return ch;
}

// terminal ! 
//	while(1)
//	{
//		i=0;
//		do
//		{ 
//		  if((USART_GetFlagStatus(USART2, USART_FLAG_RXNE) != RESET)&&(i < pktBuffSize))
//		  {
//		     g_PktBuff[i] = USART_ReceiveData(USART2);
//		     USART_SendData(USART2, g_PktBuff[i++]);
//		  }   
//		}while((g_PktBuff[i - 1] != '\r')&&(i != pktBuffSize));
//		for(i=0; i<pktBuffSize; i++)
//		{
//			g_PktBuff[0] = i;
//		}
//	}
